/* $RCSFile: EditItemPage.cs $
 * Apache license v2 2011 by Christian Surieux
 * $Source: ?/ADNCRM.Core/UI/EditItemPage.cs $
 * $log$
 * Revision 6 2010/08/06 19:56:29  christian.surieux
 *   Added Log to All cs files
 */

using System;
using System.IO;
using System.Configuration;
using System.Collections;
using System.Reflection;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Globalization;

using AddonNice.Diagnostics;
using AddonNice.Core;
using AddonNice.UI;
using AddonNice.UI.WebControls;
using AddonNice.Helpers;
using AddonNice.Security;
using AddonNice.Configuration;
using AddonNice.UI.DataTypes;

using Telerik.Web.UI;

using Trace2    =   System.Diagnostics.Trace;

namespace AddonNice.UI
{
    public delegate void ProgressDelegate(string title,int countInc);
    public delegate void ProgressSetTotalSteps(int totalSteps,int nVal2);

    /// <summary>
	/// EditItemPage inherits from AddonNice.UI.StdAuxPage <br/>
	/// Used for editing pages<br/>
	/// Usage is to be inherited
	/// </summary>
	public class EditItemPage : StdAuxPage 
	{

        #region progress Management
    

        RadProgressArea _PageProgressArea   =   null;
        public RadProgressArea PageProgressArea
        {
            get
            {
                if (_PageProgressArea == null)
                    _PageProgressArea = (RadProgressArea)FindControl("RadProgressArea1");
                return _PageProgressArea;
            }
            set
            {
                _PageProgressArea = value;
            }
        }
    
    
        /// <summary>
        /// current position in setup
        /// </summary>
        public int installCount               =   0;
        /// <summary>
        /// One step for each script and each module to install
        /// </summary>
        public int totalSteps              
        {
            get
            {
                object ob = ViewState["totalSteps"];
                if (ob != null)
                    return (int)ob;
                return 0;
            }
            set
            {
                ViewState["totalSteps"] =   value;
            }
        }
        
        #endregion progress Management

        #region Install progress management

        /*
            PrimaryTotal The maximum value of the primary progress indicators. By default, RadProgressArea displays the request size. 
            PrimaryValue The current value of the primary progress indicators. By default, RadProgressArea displays the uploaded bytes count. 
            PrimaryPercent The current value of the primary progress, expressed as a percentage of PrimaryTotal. 
            SecondaryTotal The maximum value of the secondary progress indicators. By default, RadProgressArea displays the total number of files. 
            SecondaryValue The current value of the secondary progress indicators. By default, RadProgressArea displays the number of files already uploaded. 
            SecondaryPercent The current value of the secondary progress, expressed as a percentage of SecondaryTotal. 
            CurrentOperationText The description of the current operation. By default, RadProgressArea displays the name of the currently uploading file. 
            TimeEstimated The estimated time until the operation completes. 
            TimeElapsed The time that has elapsed from the beginning of the operation. 
            Speed The speed of the process. By default, RadProgressArea displays the upload speed.
         */
        public void InitProgress()
        {
            if (PageTraceSwitch.Sw.TraceVerbose)
                Trace2.WriteLine( "[V]EditItemPage InitProgress BEG.",PageTraceSwitch.Sw.Info);
            PageProgressArea.Localization.UploadedFiles     =   LocalizeHelper.Localize("PROGRESS_COMPLETED_STEPS","Completed Steps: ");
            PageProgressArea.Localization.CurrentFileName   =   LocalizeHelper.Localize("PROGRESS_STEP","Step: ");
            PageProgressArea.Localization.TotalFiles        =   LocalizeHelper.Localize("PROGRESS_TOTAL_STEPS","Total Steps: ");
            if (PageTraceSwitch.Sw.TraceVerbose)
                Trace2.WriteLine( "[V]EditItemPage InitProgress END.",PageTraceSwitch.Sw.Info);
        }

        public void StartProgress()
        {
            if (PageTraceSwitch.Sw.TraceVerbose)
                Trace2.WriteLine( "[V]EditItemPage StartProgress BEG.",PageTraceSwitch.Sw.Info);
            RadProgressContext progress     =   RadProgressContext.Current;
            //context.PrimaryTotal          =   "100";
            //context.PrimaryValue          =   "0";
            //context.PrimaryPercent        =   "0";
            progress.SecondaryTotal         =   totalSteps;
            progress.SecondaryValue         =   "0";
            progress.SecondaryPercent       =   "0";
            progress.CurrentOperationText   =   "Start";
            if (PageTraceSwitch.Sw.TraceVerbose)
                Trace2.WriteLine( "[V]EditItemPage StartProgress END.",PageTraceSwitch.Sw.Info);
        }

        public void EndProgress()
        {
            if (PageTraceSwitch.Sw.TraceVerbose)
                Trace2.WriteLine( "[V]EditItemPage EndProgress.",PageTraceSwitch.Sw.Info);
            RadProgressContext.Current.OperationComplete = true;
        }

        /// <summary>
        /// When the total number of items change, call this to reset progress display
        /// </summary>
        public void SetTotalStepsProgress(int newTot,int secVal)
        {
            if ( newTot == 0 )
                return;
            totalSteps                      =   newTot;
            RadProgressContext progress     =   RadProgressContext.Current;
            progress.SecondaryTotal         =   totalSteps;
            progress.SecondaryValue         =   secVal.ToString();
            progress.SecondaryPercent       =   (secVal*100/totalSteps).ToString();
        }

        /// <summary>
        /// Used to change the progress display with a new value and text
        /// </summary>
        public void SetUpdateProgress(string text,int secVal)
        {
            RadProgressContext progress     =   RadProgressContext.Current;
            //progress.PrimaryValue         =   primVal.ToString();
            //progress.PrimaryPercent       =   primVal.ToString();
            progress.SecondaryValue         =   secVal.ToString();
            progress.SecondaryPercent       =   (secVal*100/totalSteps).ToString();

            progress.CurrentOperationText   =   text;

            //Cancel button was clicked or the browser was closed, so stop processing
            if (!Response.IsClientConnected)
            {
                if (PageTraceSwitch.Sw.TraceVerbose)
                    Trace2.WriteLine( string.Format(CultureInfo.InvariantCulture,"[V]EditItemPage SetUpdateProgress Cancel clicked text: {0}, secVal: {1}.", text,secVal),PageTraceSwitch.Sw.Info);
                throw new CancelProgressException();
            };
        }

        #endregion Install progress management
    
        protected EditItemPage() 
        {
            DefaultUrlReferrerToPageID  =   true;
        }

        protected EditItemPage(bool HasUpd,bool HasCancel,bool HasSaveClose,bool HasDelete) : base(HasUpd,HasCancel,HasSaveClose,HasDelete)
        {
            DefaultUrlReferrerToPageID  =   true;
        }

         /// <summary>
		/// Load settings: we check that if an entity is selected, we have rights to add or update it
        /// </summary>
        protected override void LoadSettings()
        {
            if (PageTraceSwitch.Sw.TraceVerbose)
                Trace2.WriteLine( string.Format(CultureInfo.InvariantCulture,"[V]EditItemPage LoadSettings BEG IsUnderCRM: {0}, EntityName: '{1}'.",IsUnderCRM,EntityName), PageTraceSwitch.Sw.Info);
            // Verify that the current user has access to edit this entity if uder Crm
			if ( IsUnderCRM &&  !string.IsNullOrEmpty(EntityName) && 
                ( !PortalSecurity.HasUpdatePermissions(EntityName) ||  !PortalSecurity.HasAddPermissions(EntityName) ) )
				PortalSecurity.AccessDeniedEdit();
            base.LoadSettings();
            if (PageTraceSwitch.Sw.TraceVerbose)
                Trace2.WriteLine("[V]EditItemPage LoadSettings END.", PageTraceSwitch.Sw.Info);
       }
	}
}